package com.wbao.dianniu.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.logical.Const;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.utils.Notification;
import com.wbao.dianniu.R;

public class SexEdit extends BaseActivity {

    private RadioGroup mGroup;
    private RadioButton boyRadio;
    private RadioButton girlRadio;
    private int sex = -1;
    private Button rightBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.sex_edit);
        setTitleName("性别");
        mGroup = (RadioGroup) findViewById(R.id.sex_radioGroup);
        boyRadio = (RadioButton) findViewById(R.id.sex_boy_radiobutton);
        girlRadio = (RadioButton) findViewById(R.id.sex_girl_radiobutton);
        mGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.sex_boy_radiobutton) {
                    sex = 1;
                } else if (checkedId == R.id.sex_girl_radiobutton) {
                    sex = 0;
                }
            }
        });
        sex = getIntent().getIntExtra(Const.INTENT_SEX, -1);
        if (-1 != sex) {
            if (1 == sex) {
                boyRadio.setChecked(true);
            } else if (0 == sex) {
                girlRadio.setChecked(true);
            }
        }
        rightBtn = getRightTextButton();
        rightBtn.setText(getResources().getString(R.string.save));
        rightBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_button:
                this.finish();
                break;
            case R.id.right_button:    //保存
                if (-1 == sex) {
                    Notification.toast(this, "性别不能为空");
                    return;
                }
                Intent intent = getIntent();
                // 把返回数据存入Intent
                intent.putExtra(Const.INTENT_SEX, sex);
                // 设置返回数据
                setResult(RESULT_OK, intent);
                this.finish();
                break;
            default:
                break;
        }

    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
